﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace Paxman.Misc
{
    /// <summary>
    /// Faz a leitura dos controles (teclado, mouse e joystick)
    /// </summary>
    public static class Controls
    {
        // estados dos controles
        static KeyboardState oldKeyboardState;
        static KeyboardState newKeyboardState;
        static MouseState oldMouseState;
        static MouseState newMouseState;

        /// <summary>Posição X do mouse na tela</summary>
        public static int MouseX
        {
            get { return newMouseState.X; }
        }

        /// <summary>Posição Y do mouse na tela</summary>
        public static int MouseY
        {
            get { return newMouseState.Y; }
        }

        public static void GetState()
        {
            newKeyboardState = Keyboard.GetState();
            newMouseState = Mouse.GetState();
        }

        public static void SetOldState()
        {
            oldKeyboardState = newKeyboardState;
            oldMouseState = newMouseState;
        }

        /// <summary>Verifica se uma tecla está continuamente pressionada</summary>
        public static bool IsKeyDown(Keys key)
        {
            return newKeyboardState.IsKeyDown(key);
        }

        /// <summary>Verifica se uma tecla foi pressionada uma vez</summary>
        public static bool IsKeyDownOnce(Keys key)
        {
            return (newKeyboardState.IsKeyDown(key) && !oldKeyboardState.IsKeyDown(key));
        }


        /// <summary>
        /// Verifica se o botão esquerdo do mouse está continuamente
        /// clicando num quadrado de colisão
        /// </summary>
        /// <param name="collisionRect">Quadrado de colisão</param>
        public static bool isMouseLeftButtonDown(Rectangle collisionRect)
        {
            Point mousePoint = new Point(newMouseState.X, newMouseState.Y);

            if (newMouseState.LeftButton == ButtonState.Pressed && collisionRect.Contains(mousePoint))
                return true;
            else
                return false;
        }

        /// <summary>
        /// Verifica se o botão esquerdo do mouse clicou
        /// num quadrado de colisão
        /// </summary>
        /// <param name="collisionRect">Quadrado de colisão</param>
        public static bool isMouseLeftButtonClicked(Rectangle collisionRect)
        {
            Point mousePoint = new Point(newMouseState.X, newMouseState.Y);

            if (newMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton != ButtonState.Pressed)
            {
                if (collisionRect.Contains(mousePoint))
                    return true;
            }

            return false;
        }

        /// <summary>
        /// Verifica se o botão esquerdo do mouse clicou na tela
        /// </summary>
        public static bool isMouseLeftButtonClicked()
        {
            if (newMouseState.LeftButton == ButtonState.Pressed && oldMouseState.LeftButton != ButtonState.Pressed)
                return true;
            else
                return false;
        }
    }
}
